home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / Zippo / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  9.2 KB  |  385 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Menu.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11.  
  12.  
  13. /*****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  18. #include "App.Common.h"        /* Get the stuff in common with rez.            */
  19. #include "App.protos.h"        /* Get the prototypes for application.            */
  20.  
  21. #ifndef __DESK__
  22. #include <Desk.h>
  23. #endif
  24.  
  25. #ifndef __ERRORS__
  26. #include <Errors.h>
  27. #endif
  28.  
  29. #ifndef __MEMORY__
  30. #include <Memory.h>
  31. #endif
  32.  
  33. #ifndef __MENUS__
  34. #include <Menus.h>
  35. #endif
  36.  
  37. #ifndef __TOOLUTILS__
  38. #include <ToolUtils.h>
  39. #endif
  40.  
  41. #ifndef __UTILITIES__
  42. #include "Utilities.h"
  43. #endif
  44.  
  45.  
  46.  
  47. /*****************************************************************************/
  48.  
  49.  
  50.  
  51. extern Boolean    gQuitApplication;
  52. extern Boolean    gHasAppleEvents;
  53.  
  54. static Boolean    DoAdjustFileMenu(WindowPtr window);
  55. static Boolean    DoAdjustEditMenu(WindowPtr window);
  56.  
  57. #define kMinBlockOfRam 0x10000L
  58.  
  59.  
  60.  
  61. /*****************************************************************************/
  62. /*****************************************************************************/
  63.  
  64.  
  65.  
  66. /* •• Called by DTS.Lib..framework. •• */
  67.  
  68. /* Enable and disable menus based on the current state.  The user can only
  69. ** select enabled menu items.  We set up all the menu items before calling
  70. ** MenuSelect or MenuKey, since these are the only times that a menu item can
  71. ** be selected.  Note that MenuSelect is also the only time the user will see
  72. ** menu items.  This approach to deciding what enable/disable state a menu
  73. ** item has the advantage of concentrating all the decision-making in one
  74. ** place, as opposed to being spread throughout the application.  Other
  75. ** application designs may take a different approach that is just as valid. */
  76.  
  77. #pragma segment Menu
  78. void    DoAdjustMenus(void)
  79. {
  80.     WindowPtr    window;
  81.     Boolean        redrawMenuBar;
  82.  
  83.     window = FrontWindow();
  84.  
  85.     redrawMenuBar  = DoAdjustFileMenu(window);
  86.     redrawMenuBar |= DoAdjustEditMenu(window);
  87.  
  88.     if (redrawMenuBar)
  89.         DrawMenuBar();
  90. }
  91.  
  92.  
  93.  
  94. /*****************************************************************************/
  95.  
  96.  
  97.  
  98. /* •• Called by DTS.Lib..framework. •• */
  99.  
  100. /* This is called when an item is chosen from the menu bar (after calling
  101. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  102. ** It is good to have both the result of MenuSelect and MenuKey go to one
  103. ** routine like this to keep everything organized. */
  104.  
  105. #pragma segment Menu
  106. void    DoMenuCommand(long menuResult)
  107. {
  108.     short        menuID, menuItem, undoDepth, numUndos, saveMode, daRefNum;
  109.     Str255        daName;
  110.     FileRecHndl    frHndl;
  111.     WindowPtr    window;
  112.     OSErr        err;
  113.  
  114.     if (window = FrontWindow())
  115.         frHndl = (FileRecHndl)GetWRefCon(window);
  116.             /* frHndl is valid only if it is one of our windows. */
  117.  
  118.     menuID   = HiWord(menuResult);
  119.     menuItem = LoWord(menuResult);
  120.  
  121.     switch (menuID) {
  122.  
  123.         case mApple:
  124.             switch (menuItem) {
  125.                 case iAbout:    /* Bring up alert for About. */
  126.                     CenteredAlert(rAboutAlert, nil, (ModalFilterProcPtr)AlertFilter);
  127.                     break;
  128.                 default:        /* All non-About items in this menu are DAs. */
  129.                     GetItem(GetMHandle(mApple), menuItem, daName);
  130.                     daRefNum = OpenDeskAcc(daName);
  131.                     break;
  132.             }
  133.             break;
  134.  
  135.         case mFile:
  136.             switch (menuItem) {
  137.                 case iNew:
  138.                     err = NewDocument(&frHndl, kDocFileType, true);
  139.                     if (!err) {
  140.                         err = DoNewWindow(frHndl, nil, FrontWindow(), (WindowPtr)-1);
  141.                         if (err)
  142.                             DisposeDocument(frHndl);
  143.                     }
  144.                     if (err)
  145.                         CenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  146.                     break;
  147.                 case iOpen:
  148.                     err = OpenDocument(&frHndl, nil, fsRdWrPerm);
  149.                     if (!err) {
  150.                         err = DoNewWindow(frHndl, nil, FrontWindow(), (WindowPtr)-1);
  151.                         if (err)
  152.                             DisposeDocument(frHndl);
  153.                     }
  154.                     if ((err) && (err != userCanceledErr))
  155.                         CenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  156.                     break;
  157.                 case iClose:
  158.                     DisposeOneWindow(window, kClose);
  159.                     break;
  160.                 case iSave:
  161.                 case iSaveAs:
  162.                     saveMode = (menuItem == iSave) ? kSave : kSaveAs;
  163.                     if ((*frHndl)->fileState.refNum == kInvalRefNum)
  164.                         saveMode = kSaveAs;
  165.                     err = SaveDocument(frHndl, window, saveMode);
  166.                     if ((err) && (err != userCanceledErr))
  167.                         CenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  168.                     break;
  169.                 case iPageSetup:
  170.                     DoSetCursor(&qd.arrow);
  171.                     PresentStyleDialog(frHndl);
  172.                     break;
  173.                 case iPrint:
  174.                     DoSetCursor(&qd.arrow);
  175.                     err = noErr;
  176.                     if (!(*frHndl)->d.doc.fhInfo.printRecValid)
  177.                         err = PresentStyleDialog(frHndl);
  178.                     if (!err) {
  179.                         err = PrintDocument(frHndl, true, true);
  180.                         PrintDocument(nil, false, false);
  181.                     }
  182.                     if ((err) && (err != userCanceledErr))
  183.                         CenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  184.                     break;
  185.                 case iQuit:
  186.                     gQuitApplication = DisposeAllWindows();
  187.                     break;
  188.             }
  189.             break;
  190.  
  191.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  192.             if (IsAppWindow(window)) {
  193.                 switch (menuItem) {
  194.                     case iUndo:
  195.                     case iRedo:
  196.                     case iCut:
  197.                     case iCopy:
  198.                     case iPaste:
  199.                     case iClear:
  200.                         switch ((*frHndl)->fileState.sfType) {
  201.                             case kDocFileType:
  202.                                 if (menuItem <= iRedo) {
  203.                                     if (iUndo == iRedo) {
  204.                                         GetUndoInfo(frHndl, &undoDepth, &numUndos);
  205.                                         DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
  206.                                     }
  207.                                     else DoUndoTask((*frHndl)->d.doc.root, menuItem - iUndo, true);
  208.                                 }
  209.                                 else {
  210.                                     /* Handle rest of edit menu here. */
  211.                                 }
  212.                                 break;
  213. #if VH_VERSION
  214.                             case kViewHierFileType:
  215.                                 BeginContent(window);
  216.                                 if (menuItem == iUndo)
  217.                                     CTEUndo();
  218.                                 else
  219.                                     CTEClipboard(menuItem - iCut + 2);
  220.                                 EndContent(window);
  221.                                 break;
  222. #endif
  223.                         }
  224.                         break;
  225. #if VH_VERSION
  226.                     case iViewHier:
  227.                         err = NewDocument(&frHndl, kViewHierFileType, false);
  228.                         if (!err) {
  229.                             err = DoNewWindow(frHndl, nil, FrontWindow(), (WindowPtr)-1);
  230.                             if (err)
  231.                                 DisposeDocument(frHndl);
  232.                         }
  233.                         if (err)
  234.                             CenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  235.                         break;
  236. #endif
  237.                 }
  238.             }
  239.             else SystemEdit(menuItem - 1);
  240.             break;
  241.  
  242.     }
  243.  
  244.     HiliteMenu(0);        /* Unhighlight what MenuSelect (or MenuKey) hilited. */
  245. }
  246.  
  247.  
  248.  
  249. /*****************************************************************************/
  250.  
  251.  
  252.  
  253. /* This function either enables or disables a menu item. */
  254.  
  255. #pragma segment Menu
  256. void    EnableOrDisableItem(MenuHandle menu, short item, Boolean enable)
  257. {
  258.     if (enable)
  259.         EnableItem(menu, item);
  260.     else
  261.         DisableItem(menu, item);
  262. }
  263.  
  264.  
  265.  
  266. /*****************************************************************************/
  267.  
  268.  
  269.  
  270. #pragma segment Menu
  271. static Boolean    DoAdjustFileMenu(WindowPtr window)
  272. {
  273.     MenuHandle    menu;
  274.     FileRecHndl    frHndl;
  275.     short        i, enableItem;
  276.  
  277.     menu = GetMHandle(mFile);
  278.  
  279.     if (IsDAWindow(window)) {
  280.         for (i = iNew; i < iQuit; ++i) DisableItem(menu, i);
  281.         EnableItem(menu, iClose);    /* Let DAs do a close from the menu. */
  282.         return(false);
  283.     }
  284.  
  285.     EnableItem(menu, iNew);                /* Set these for the no-windows state. */
  286.     EnableItem(menu, iOpen);
  287.     if (MaxBlock() < kMinBlockOfRam) {
  288.         if (CompactMem(kMinBlockOfRam) < kMinBlockOfRam) {
  289.             DisableItem(menu, iNew);    /* Running low on RAM. */
  290.             DisableItem(menu, iOpen);
  291.         }
  292.     }
  293.  
  294.     DisableItem(menu, iClose);
  295.     DisableItem(menu, iSave);
  296.     DisableItem(menu, iSaveAs);
  297.     DisableItem(menu, iPageSetup);
  298.     DisableItem(menu, iPrint);
  299.  
  300.     if (IsDAWindow(window)) {
  301.         DisableItem(menu, iNew);    /* DAs don't get to do a new. */
  302.         DisableItem(menu, iOpen);    /* DAs don't get to do an open. */
  303.         EnableItem(menu, iClose);    /* Let DAs do a close from the menu. */
  304.     }
  305.  
  306.  
  307.     if (window = FrontWindowOfType(kwIsDocument)) {
  308.         EnableItem(menu, iClose);
  309.         frHndl = (FileRecHndl)GetWRefCon(window);
  310.         if ((*frHndl)->fileState.sfType == kDocFileType) {
  311.             enableItem = GetWindowDirty(window);
  312.             if ((*frHndl)->fileState.refNum == kInvalRefNum)
  313.                 enableItem = true;
  314.             EnableOrDisableItem(menu, iSave, enableItem);
  315.             EnableItem(menu, iSaveAs);
  316.         }
  317.         EnableItem(menu, iPageSetup);
  318.         EnableItem(menu, iPrint);
  319.     }
  320.  
  321.     return(false);
  322. }
  323.  
  324.  
  325.  
  326. /*****************************************************************************/
  327.  
  328.  
  329.  
  330. #pragma segment Menu
  331. static Boolean    DoAdjustEditMenu(WindowPtr window)
  332. {
  333.     MenuHandle        menu;
  334.     Boolean            menuEnabled, redrawMenuBar;
  335.     FileRecHndl        frHndl;
  336.     short            i, j;
  337.     static Boolean    editMenuEnabled = true;
  338.  
  339.     menu = GetMHandle(mEdit);
  340.  
  341.     if (IsDAWindow(window)) {
  342.         EnableItem(menu, iUndo);
  343.         EnableItem(menu, iCut);
  344.         EnableItem(menu, iCopy);
  345.         EnableItem(menu, iPaste);
  346.         EnableItem(menu, iClear);
  347. #if VH_VERSION
  348.         DisableItem(menu, iViewHier);
  349. #endif
  350.         redrawMenuBar   = !editMenuEnabled;
  351.         editMenuEnabled = true;
  352.         return(redrawMenuBar);
  353.     }
  354.  
  355. #if VH_VERSION
  356.     j = iViewHier;
  357. #else
  358.     j = iClear;
  359. #endif
  360.  
  361.     menuEnabled = false;
  362.     for (i = iUndo; i <= j; ++i) DisableItem(menu, i);
  363.     if (IsAppWindow(window)) {
  364.         frHndl = (FileRecHndl)GetWRefCon(window);
  365.         switch ((*frHndl)->fileState.sfType) {
  366.             case kDocFileType:
  367.                 EnableItem(menu, iViewHier);
  368.                 menuEnabled = true;
  369.                 break;
  370. #if VH_VERSION
  371.             case kViewHierFileType:
  372.                 CTEEditMenu(&menuEnabled, mEdit, iUndo, iCut);
  373.                 break;
  374. #endif
  375.         }
  376.     }
  377.  
  378.     redrawMenuBar   = (editMenuEnabled != menuEnabled);
  379.     editMenuEnabled = menuEnabled;
  380.     return(redrawMenuBar);
  381. }
  382.  
  383.  
  384.  
  385.